<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Goods as GoodsM;
use app\common\model\WalletToken;
use think\Db;

/**
 * 钱包业务接口
 */
class Wallet extends Api
{
    /**
     * 钱包列表
     */
    public function wallet_list()
    {
        $list = \app\common\model\Wallet::user_wallet($this->auth->id);
        foreach ($list as &$item) {
            $coin_info = WalletToken::get($item['coin_id']);
            $item['coin_name'] = $coin_info['title_en'];
            $item['coin_price_cn'] = $coin_info['price_cn'];
            $item['coin_price_usd'] = $coin_info['price_usd'];
            $item['coin_logo'] = $coin_info['logo'];
            $item['withdraw_fee'] = $coin_info['withdraw_fee'];
            $item['withdraw_type'] = $coin_info['withdraw_type'];
        }

        $this->success('返回成功', $list);
    }

    /**
     * 转账接口
    */
    public function transfer_coin()
    {
        if ($this->request->isPost()) {
            $num = input('num', 0, 'intval');
            $coin_id = input('coin_id', 0, 'intval');
            $to_uid = input('to_uid', '', 'trim');
            if ($num > 0) {
                if ($coin_id <= 0) {
                    return $this->error('币种ID错误');
                } elseif (empty($to_uid)) {
                    return $this->error('转入会员ID不能为空');
                } else {
                    $res = \app\common\model\Wallet::transfer_coin($this->auth->id, $to_uid, $coin_id, $num);
                    if ($res['error'] > 0) {
                        return $this->error($res['msg']);
                    } else {
                        return $this->success('转账成功');
                    }
                }
            } else {
                return $this->error('转账数量错误');
            }
        }
    }

    /**
     * 提现接口
     */
    public function withdraw_coin()
    {
        if ($this->request->isPost()) {
            $num = input('num', 0, 'intval');
            $coin_id = input('coin_id', 0, 'intval');
            $address = input('address', '', 'trim');
            if ($num > 0) {
                if ($coin_id <= 0) {
                    return $this->error('币种ID错误');
                } elseif (empty($address)) {
                    return $this->error('提现地址不能为空');
                } else {
                    $res = \app\common\model\Wallet::withdraw_coin($this->auth->id, $address, $coin_id, $num);
                    if ($res['error'] > 0) {
                        return $this->error($res['msg']);
                    } else {
                        return $this->success('提现申请成功');
                    }
                }
            } else {
                return $this->error('提现数量错误');
            }
        }
    }

    /**
     * 获取用户地址
    */
    public function get_address()
    {
        $coin_id = input('coin_id', config('usdt_id'), 'intval');
        $address = Db::name('wallet_address')->where(['uid' => $this->auth->id, 'coin_id' => $coin_id])->value('address');
        if (empty($address)) {
            $address_info = \app\common\model\Wallet::create_address($this->auth->id, $coin_id);
            if ($address_info['error'] > 0) {
                $this->error($address['msg']);
            } else {
                $address = $address_info['data'];
            }
        }

        return $this->success('返回成功', $address);
    }

    /**
     * 充值接口
    */
    public function get_recharge()
    {
        $res = [];
        if ($this->request->isPost()) {
            $wallet = Db::name('wallet')->where('uid', $this->auth->id)->select();
            foreach ($wallet as $item) {
                if ($item['coin_id'] == config('usdt_id')) {
                    $res = \app\common\model\Wallet::get_recharge($this->auth->id, $item['coin_id']);
                }
            }
        }

        return $this->success('返回成功', $res);
    }
}
